<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;
use App\Helper\Calendar;
use App\Service\TradService;

/**
 * 用户服务
 */
class UserService 
{
    private $user_id = 0;
    /**
     * [__construct description] 构造函数
     */
    public function __construct($user_id=0)
    {
        $this->user_id = $user_id;
    }
    /**
     * 设置全局用户ID
     * [setUserId description]
     * @param [type] $user_id [description]
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
    }
    /**
     * 获取用户信息
     * [getUserInfo description]
     * @return [type] [description]
     */
    public function getUserInfo()
    {
        $return_data = [
            'nickname'=>NULL,
            'avatar'=>NULL
        ];
        $user = Db::table('user')
            ->where('id', $this->user_id)
            ->first();
        if(empty($user)){
            return $return_data;
        }
        $return_data['nickname'] = $user['nickname'];
        $return_data['avatar'] = $user['avatar'];
        return $return_data;
    }
    /**
     * 保存用户信息
     * [saveUserInfo description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function saveUserInfo($params)
    {
        if(!empty($this->user_id)){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['nickname'])){
                $save_data['nickname'] = $params['nickname'];
            }
            if(!empty($params['avatar'])){
                $save_data['avatar'] = $params['avatar'];
            }
            Db::table('user')
                ->where(['id'=>$this->user_id])
                ->update($save_data);
        }
        return true;
    }
    /**
     * 检查关注状态
     * [checkSubStatus description]
     * @return [type] [description]
     */
    public function checkSubStatus()
    {
        $return_data = [
            'qpp'=>0,
            'wechat_name'=>'抢票票助手',
            'wechat_example_image'=>env('QINIU_URL').'app/img/other/find_shoes_wxgzh.png',
            'mini_program_param'=>[
                'appid'=>'gh_225d80df7486',
                'path'=>'pages/users/qrcode/index'
            ]
        ];
        $user = Db::table('user')
            ->where('id', $this->user_id)
            ->first();
        if(empty($user)){
            return $return_data;
        }
        if(!empty($user['wx_openid'])){
            $user_act = Db::table('wxship_act')
                ->where('openid', $user['wx_openid'])
                ->first();
            if(!empty($user_act['status']) && $user_act['status']==1){
                $return_data['qpp'] = 1;
            }
        }elseif(!empty($user['unionid'])){
            $user_act = Db::table('wxship_act')
                ->where('unionid', $user['unionid'])
                ->first();
            if(!empty($user_act['status']) && $user_act['status']==1){
                $return_data['qpp'] = 1;
            }
        }
        return $return_data;
    }
    /**
     * 注销
     * [logoff description]
     * @return [type] [description]
     */
    public function logoff()
    {
        if(!empty($this->user_id)){
            $save_data = [
                'updated_at'=>time(),
                'is_del'=>1
            ];
            Db::table('user')
                ->where(['id'=>$this->user_id])
                ->update($save_data);
        }
        return true;
    }
    /**
     * 关于我们
     * [aboutus description]
     * @return [type] [description]
     */
    public function aboutus()
    {
        $data = [
            "about_us"=>"华语明星演唱会门票订阅助手，订阅心仪的明星演唱会，热门现场想去就去。
甄选海量明星演出现场，让你抢先了解更多优质音乐Live信息。
热门演出，精准毫秒级卡点加速抢票，可以轻松订购心仪的明星演唱会门票。",
            "website"=>"http://qpphome.51sqbby.com",
            "weixin"=>"qppzs01",
            "email"=>"tanxie2023@163.com",
            "phone"=>"028-62029661"
        ];
        return $data;
    }
    /**
     * 协议
     * [protocol description]
     * @return [type] [description]
     */
    public function protocol()
    {
        $data = [
            //用户协议
            'user_agreement' => 'https://qpphome.51sqbby.com/register.html',
            //隐私政策
            'privacy_policy' => 'https://qpphome.51sqbby.com/privacy.html',
            //账户注销须知
            'cancel_agreement'=> 'https://qpphome.51sqbby.com/logout.html'
        ];
        return $data;
    }
}
